// pages/setTopic/choseItem/choseItem.js
var app = getApp();


function countSelect(t){
  var cunt = 0;
  console.log(t.data.choseItem)
  for (var i = 0;i<t.data.choseItem.length;i++){
    if (t.data.qd.question_data[i].selected == true ) {
      cunt++
    }
  };
  if (t.data.qd.question_data.length == cunt){
    var res={cunt: cunt, all:true}
  }else{
    var res = { cunt: cunt, all: false }
  }
  return res ;
};


Page({

  /**
   * 页面的初始数据
   */
  data: {
    all:false,
    isFold:true,
    animation:{},
    letters: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'N', 'M', 'O'],
    countSelect:0,
    qd:'',
    choseItem: [],
    company: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中...',
    })
    var that = this;
    wx.request({
      url: app.globalData.phpUrl + 'user/api',
      method: 'POST',
      data: { api_name: 'naire_detail', openid: app.globalData.userData.openOId, questionnaire_id: options.id },
      success: function (d) {
        
        var qd = d.data.data;
        for (var o = 0; o < qd.question_data.length;o++){
          qd.question_data[o].selected = false;
          qd.question_data[o].id = 0;
          for (var uu = 0; uu < qd.question_data[o].options.length;uu++){
            delete qd.question_data[o].options[uu].pid;
            qd.question_data[o].options[uu].id = 0;
            delete qd.question_data[o].options[uu].option_select;
            delete qd.question_data[o].options[uu].headimg_arr;
          }
        };

        var choseItem = []
        for (var dd = 0; dd < qd.question_data.length; dd++){
          choseItem[dd] = { value: dd+1, selected: false }
        }
      
        that.setData({
          questionnaire_id: qd.questionnaire_id,
          qd: qd,
          company: qd.is_company,
          choseItem: choseItem
        });

        wx.hideLoading();
        console.log(that.data.qd)
        var query = wx.createSelectorQuery();
        query.select('#titleTxt').boundingClientRect()
        query.exec(function (res) {
          if (res[0].width < 250) {
            that.setData({
              isFold: false
            });
          }
        });
        that.animation = wx.createAnimation({
          duration: 1000,
          transformOrigin: "100% 0",
          timingFunction: "ease",
        });
        that.setData({
          animation: that.animation.export(),
        });
      }
    });


  },

  moreTtile: function () {
    var that = this;
    that.setData({
      isFold: false
    });
    var query = wx.createSelectorQuery();
    query.select('#moreContent').boundingClientRect();
    query.exec(function (res) {
      that.animation.bottom(- res[0].height).step({ duration: 300 });
      that.setData({
        animation: that.animation.export(),
      });
    })

  },

  defaultTtile: function () {
    var that = this;
    that.setData({
      isFold: true
    });
    that.animation.bottom('0rpx').step({ duration: 300 });
    that.setData({
      animation: that.animation.export(),
    });
  },

  bindCheckbox:function(e){
    var that = this;
    var selected =  "qd.question_data["+e.currentTarget.dataset.index+"].selected";
    console.log(selected);
    that.setData({
      [selected] : true,
    });
    var res =  countSelect(that);
    that.setData({
      countSelect: res.cunt,
      all: res.all,
    })
  },

  bindCheckboxNo: function (e) {
    var that = this;
    var selected = "qd.question_data[" + e.currentTarget.dataset.index + "].selected";
    console.log(selected);
    that.setData({
      [selected]: false,
    });
    var res = countSelect(that);
    that.setData({
      countSelect: res.cunt,
      all: res.all,
    })
  },

  selectAll:function(){
    var that = this;
    for (var i = 0; i < that.data.qd.question_data.length; i++) {
      var selected = "qd.question_data[" + i + "].selected";
      that.setData({
        [selected]: true,
      });
    };
    var res = countSelect(that);
    that.setData({
      countSelect: res.cunt,
      all: res.all,
    })
  },

  cancelAll: function () {
    var that = this;
    for (var i = 0; i < that.data.qd.question_data.length; i++) {
      var selected = "qd.question_data[" + i + "].selected";
      that.setData({
        [selected]: false,
      });
    };
    var res = countSelect(that);
    that.setData({
      countSelect: res.cunt,
      all: res.all,
    })
  },

  confirm:function(){
    var that = this;
    var confirmData = [];
    for (var p = 0; p < that.data.qd.question_data.length;p++){
      if (that.data.qd.question_data[p].selected){
        delete that.data.qd.question_data[p].selected;
        that.data.qd.question_data[p].useId = that.data.questionnaire_id;
        that.data.qd.question_data[p].type = that.data.qd.question_data[p].type - 1;
        confirmData.push(that.data.qd.question_data[p]);
      }
    };

    var agg = getCurrentPages()[0].data.subject.question.concat(confirmData);
    var useArr = getCurrentPages()[0].data.useArr
        useArr.push(that.data.questionnaire_id)
    getCurrentPages()[0].setData({
      ['subject.question']: agg,
      ['useArr']: useArr
    });
    
    wx.navigateBack({
      delta: 2
    })
  }
})